<template>
  <transition name="slide">
    <div class="like">
      <div class="header">
        <div class="back" @click="back()">
          <i class="icon-back"></i>
        </div>
        <div class="title">
          {{ title }}
        </div>
      </div>
      <!-- 列表加载 -->
      <scroll :list="list" class="wrapper" ref="scroll" :probeType="probeType" :listenScroll="listenScroll">
        <div class="content">
          <h1 class="title" v-show="list.length >0" @click="showConfirm">
            <span class="text">清空列表</span>
            <span class="clear">
              <i class="icon-clear"></i>
            </span>
          </h1>
          <music-item :songs="list"></music-item>
        </div>
      </scroll>
      <!-- 数据加载时 -->
      <div class="noDate" v-show="list.length == 0 || list == undefined">
        <img src="@/assets/noDate.png">
      </div>
      <confirm ref="confirm" :text="confirmText" confirmBtnText="清空"
                    @confirm="clearAll"
            ></confirm>
    </div>
  </transition>
</template>

<script>
import Scroll from '@/components/base/Scroll'
import Confirm from '@/components/base/Confirm'
import MusicItem from '@/components/common/MusicItem'
// import BScroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
import { playListMixin } from '../../common/js/mixin'
export default {
  mixins: [playListMixin],
  components: {
    Scroll,
    Confirm,
    MusicItem
  },
  data () {
    return {
      probeType: 1,
      listenScroll: true
    }
  },
  computed: {
    ...mapState(['likeMusic', 'historyPlay']),
    title () {
      return this.$route.name === 'MineLike' ? '我喜欢' : '播放历史'
    },
    confirmText () {
      return this.$route.name === 'MineLike' ? '是否清空所有喜欢的歌曲' : '是否清空所有的播放历史'
    },
    list () {
      // console.log(this.likeMusic)  //暂时没有
      return this.$route.name === 'MineLike' ? this.likeMusic : this.historyPlay
    }
  },
  methods: {
    ...mapMutations(['setCurrentIndex', 'setPlayList', 'setSequenceList', 'setFullScreen', 'clearlikeMusic', 'clearHistoryPlay']),
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      // list是个vue components，.$el取元素
      this.$refs.scroll.$el.style.bottom = bottom
      this.$refs.scroll.refresh()
    },
    back () {
      this.$router.back()
    },
    // 清除所有的
    clearAll () {
      if (this.$route.name === 'MineLike') {
        localStorage.removeItem('likeMusic')
        this.clearlikeMusic()
      } else {
        localStorage.removeItem('historyPlay')
        this.clearHistoryPlay()
      }
    },
    showConfirm () {
      this.$refs.confirm.show()
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '../../common/stylus/variable'
.slide-enter, .slide-leave-to {
  transform: translateX(100%);
}
.slide-enter-active, .slide-leave-active {
  transition: all 0.5s ease;
}
.like {
  position: fixed;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background-color: $color-background;
  .header {
    background: $color-background;
    .back {
      position:absolute;
      top: 10px;
      left: 10px;
      font-size: 22px;
      color: $color-theme;
    }
    .title {
      width: 60%;
      line-height: 44px;
      font-size: 18px;
      text-align: center;
      margin: 0 auto;
      color: $color-theme;
    }
  }
  .wrapper {
    position: fixed;
    top: 30px;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 20px 20px 0;
    .title {
      display: flex;
      align-items:center;
      justify-content: flex-end;
      padding: 0 20px;
      line-height: 24px;
      color:$color-theme;
      .text {
        margin-right: 6px;
      }
    }
  }
  .noDate {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }
}
</style>
